package main

// $ go mod init github.com/zhangxi/rpc_goods_client
// $ ==> go mod init rpc_goods_client
// $ go mod tidy  #==> 下载项目中引入的第三方包,

import (
	"fmt"
	"net/rpc"
)

type AddGoodsReq struct {
	ID      int
	Title   string
	Price   float32
	Content string
}

type AddGoodsResp struct {
	Success bool
	Message string
}

type GetGoodsReq struct {
	ID int
}

type GetGoodsResp struct {
	ID      int
	Title   string
	Price   float32
	Content string
}

func main() {
	// 1.用 rpc.Dial() 和 rpc 微服务端建立连接,
	conn, err1 := rpc.Dial("tcp", "127.0.0.1:8000")
	if err1 != nil {
		fmt.Println("rpc.Dial() err:", err1)
	}
	// 2.当客户端退出的时候关闭连接,
	defer conn.Close()
	// 3.调用远程函数,
	var req = AddGoodsReq{ID: 12, Title: "AddGoods", Price: 32.55, Content: "hahaha"}
	var reply AddGoodsResp
	err2 := conn.Call("goods_service.AddGoods", req, &reply)
	if err2 != nil {
		fmt.Println("conn.Call() err:", err2)
		return
	}
	// 4.获取微服务返回的数据,
	fmt.Printf("%#v\n", reply)

	// 5.调用远程函数,
	var req2 = GetGoodsReq{ID: 12}
	var reply2 GetGoodsResp
	err3 := conn.Call("goods_service.GetGoods", req2, &reply2)
	if err3 != nil {
		fmt.Println("conn.Call() err:", err3)
		return
	}
	// 6.获取微服务返回的数据,
	fmt.Printf("%#v\n", reply2)

}
